from sqlalchemy import Column, Integer, String, DateTime, Float, Boolean
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class UrlList(Base):
    __tablename__ = "url_lists"
    id = Column(Integer, primary_key=True, autoincrement=True)
    keyword = Column(String(255), nullable=False)
    url = Column(String(255), nullable=False)
    product_name = Column(String(255))
    product_price = Column(Float)
    product_sales = Column(Integer)
    type = Column(Integer)
    is_valid = Column(Boolean)
    add_time = Column(DateTime)
    update_time = Column(DateTime)

    def to_json(self):
        return dict(id=self.id,
                    add_time=self.add_time.isoformat(),
                    url=self.url,
                    product_name=self.product_name,
                    product_price=self.product_price,
                    product_sales=self.product_sales,
                    is_valid=self.is_valid,
                    update_time=self.update_time.isoformat())


class KeywordData(Base):
    __tablename__ = "keywords"
    id = Column(Integer, primary_key=True, autoincrement=True)
    keyword = Column(String(255), unique=True, nullable=False)
    average_price = Column(Float)
    average_origin_price = Column(Float)
    tmp_average_price = Column(Float)
    tmp_average_origin_price = Column(Float)
    tandi_price = Column(Float)
    profit = Column(Float, default=0.05)
    add_time = Column(DateTime)
    img_name = Column(String(255), unique=True)
    purchase_url = Column(String(255), unique=True, nullable=False)

    def to_json(self):
        return dict(keyword=self.keyword,
                    add_time=self.add_time.isoformat(),
                    profit=self.profit,
                    average_price=self.tmp_average_price,
                    average_origin_price=self.tmp_average_origin_price,
                    tandi_price=self.tandi_price,
                    img_name=self.img_name,
                    purchase_url=self.purchase_url)


class WechatUser(Base):
    __tablename__ = "wechat_users"
    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(255), unique=True, nullable=False, primary_key=True)
    from_username = Column(String(255))
    qr_code_media_id = Column(String(255))
    qr_code_create_time = Column(DateTime)
    time = Column(DateTime)


class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(255), unique=True, nullable=False, primary_key=True)
    password_hash = Column(String(255), nullable=False)
    nickname = Column(String(255))
    register_time = Column(DateTime)
    last_login_time = Column(DateTime)

    def hash_password(self, password):
        from run import bcrypt
        self.password_hash = bcrypt.generate_password_hash(password)

    def verify_password(self, password):
        from run import bcrypt
        return bcrypt.check_password_hash(self.password_hash, password)


class ProxyData(Base):
    __tablename__ = "proxy"
    id = Column(Integer, primary_key=True, autoincrement=True)
    proxy = Column(String(255), nullable=False, unique=True)
    isValid = Column(Boolean, nullable=False)
